import express from 'express'
import cors from 'cors'
import { DataBase } from './database.js'

const app = express()

app.use(express.json())
app.use(cors())

app.get('/queryAll', (request, response) => {
  response.send({ code: 200, data: DataBase.read() })
})

app.post('/add', (request, response) => {
  const { body } = request
  DataBase.add(body)
  response.send({ code: 200, data: null })
})

app.get('/query', (request, response) => {
  const { params } = request
  const person = DataBase.query(params.id)
  response.send({ code: 200, data: person })
})

app.post('/update', (request, response) => {
  const { params } = request
  const person = DataBase.update(params)
  response.send({ code: 200, data: person })
})

app.post('/delete', (request, response) => {
  const { body } = request
  DataBase.delete(body.id)
  response.send({ code: 200, data: null })
})

app.listen(8000, () => {
  console.log('服务器启动: http://localhost:8000')
})
